package cn.edu.seu.herald.informationplatform.dao.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.seu.herald.informationplatform.bean.league.LeagueInfo;

import com.mysql.jdbc.Statement;

public class LeagueInfoDao extends DataAccessObject{
    private static LeagueInfoDao daoInstance=null;
	
	//Constructor of LeagueDao
	protected LeagueInfoDao(){
		
	}
	
	//Get LeagueDao instance
	public static LeagueInfoDao getDao(){
		if(daoInstance==null){
			daoInstance=new LeagueInfoDao();
		}
		return daoInstance;
	}
	
	//add 
	//Return:the new item's id
	public int addLeagueInfo(LeagueInfo newLeagueInfo) throws SQLException{
		Connection con=getConnectionLeague();
		try {
			PreparedStatement ps=con.prepareStatement("insert into lg_league_info(league_name,league_introduce,register_time,last_login_time,class) values(?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
			ps.setString(1,newLeagueInfo.getName());
			ps.setString(2,newLeagueInfo.getIntroduction());
			ps.setString(3,newLeagueInfo.getRegisterTime());
			ps.setString(4,newLeagueInfo.getLastLoginTime());
			ps.setInt(5,newLeagueInfo.getLeagueClass());
			ps.execute();
			ResultSet rsResultSet=ps.getGeneratedKeys();
			int new_id=-1;
			if(rsResultSet.next()){
				new_id=rsResultSet.getInt(1);
			}
			closeRs(rsResultSet);
			return new_id;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally{
			closeCon(con);
		}	
	}
	
	//delete
	public void deleteLeagueInfo(int id) throws SQLException{
		Connection con=getConnectionLeague();
		try {
				PreparedStatement ps=con.prepareStatement("delete from lg_league_info where id=?");
				ps.setInt(1,id);
				ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally{
			closeCon(con);
		}
	}
	
	//update
	public void updateLeagueInfo(int id,LeagueInfo newLeagueInfo) throws SQLException{
		Connection con=getConnectionLeague();
		try {
				PreparedStatement ps=con.prepareStatement("update lg_league_info set league_name=?,league_introduce=?,portrait=?,class=? where id=?");
				ps.setString(1,newLeagueInfo.getName());
				ps.setString(2,newLeagueInfo.getIntroduction());
				ps.setString(3,newLeagueInfo.getPortrait());
				ps.setInt(4,newLeagueInfo.getLeagueClass());
				ps.setInt(5,id);
				
				ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally{
			closeCon(con);
		}
	}
	
	//update
	public void updateLeaguePortrait(int id,String path) throws SQLException{
		Connection con=getConnectionLeague();
		try {
				PreparedStatement ps=con.prepareStatement("update lg_league_info set portrait=? where id=?");
				ps.setString(1,path);
				ps.setInt(2,id);
				ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally{
			closeCon(con);
		}
	}
	
	//get
	//Return:object of LeagueInfo
	public LeagueInfo getLeagueInfo(int id) throws SQLException{
		Connection con=getConnectionLeague();
		try {
				LeagueInfo leagueInfo = new LeagueInfo();
				PreparedStatement ps=con.prepareStatement("select league_name,league_introduce,register_time,last_login_time,portrait,class from lg_league_info where id=?");
				ps.setInt(1,id);
				ResultSet rsResultSet=ps.executeQuery();
				while(rsResultSet.next()){
					leagueInfo.setName(rsResultSet.getString(1));
					leagueInfo.setIntroduction(rsResultSet.getString(2));
					leagueInfo.setRegisterTime(rsResultSet.getString(3));
					leagueInfo.setLastLoginTime(rsResultSet.getString(4));
					leagueInfo.setPortrait(rsResultSet.getString(5));
					leagueInfo.setLeagueClass(rsResultSet.getInt(6));
				}
				closeRs(rsResultSet);
				return leagueInfo;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally{
			closeCon(con);
		}
	}
	
}
